import urllib.request
import urllib.parse

# urlencode应用场景：多个参数的情况
# https://www.baidu.com/s?ie=UTF-8&wd=%E5%8D%83%E4%B8%8E%E5%8D%83%E5%AF%BB&type=%E5%8A%A8%E6%BC%AB

"""
data = {
    "wd":"千与千寻",
    'type':'动漫'
}

result = urllib.parse.urlencode(data)
print(result) # wd=%E5%8D%83%E4%B8%8E%E5%8D%83%E5%AF%BB&type=%E5%8A%A8%E6%BC%AB
"""

base_url = "https://www.baidu.com/s?ie=UTF-8&"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
}

data = {
    "wd":"千与千寻",
    'type':'动漫'
}
new_url = urllib.parse.urlencode(data)
url = base_url + new_url
request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf8')
print(content)